import java.net.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;

/**
 * 火币WebSocket连接测试
 * 测试火币WebSocket地址的可达性和连接性
 */
public class HuobiWebSocketTest {
    
    private static final String[] HUOBI_URLS = {
        "wss://api.huobi.pro/ws",
        "wss://api-aws.huobi.pro/ws", 
        "wss://api.hbdm.com/ws",
        "wss://api.hbdm.com/linear-swap-ws",
        "wss://api.huobi.de.com/ws",
        "wss://api.huobipro.com/ws",
        "wss://api.huobi.br.com/ws",
        "wss://api.huobi.co/ws"
    };
    
    public static void main(String[] args) {
        System.out.println("开始测试火币WebSocket连接...");
        
        for (String url : HUOBI_URLS) {
            testWebSocketConnection(url);
        }
        
        System.out.println("测试完成！");
    }
    
    private static void testWebSocketConnection(String wsUrl) {
        try {
            System.out.println("\n测试地址: " + wsUrl);
            
            // 解析WebSocket URL
            URI uri = URI.create(wsUrl);
            String host = uri.getHost();
            int port = uri.getPort() == -1 ? 443 : uri.getPort();
            
            System.out.println("  主机: " + host);
            System.out.println("  端口: " + port);
            
            // 测试TCP连接
            boolean tcpConnectable = testTcpConnection(host, port);
            System.out.println("  TCP连接: " + (tcpConnectable ? "成功" : "失败"));
            
            // 测试DNS解析
            boolean dnsResolvable = testDnsResolution(host);
            System.out.println("  DNS解析: " + (dnsResolvable ? "成功" : "失败"));
            
            // 测试HTTP连接
            boolean httpConnectable = testHttpConnection(host, port);
            System.out.println("  HTTP连接: " + (httpConnectable ? "成功" : "失败"));
            
            // 综合评估
            if (tcpConnectable && dnsResolvable) {
                System.out.println("  状态: 可用 ✓");
            } else {
                System.out.println("  状态: 不可用 ✗");
            }
            
        } catch (Exception e) {
            System.out.println("  错误: " + e.getMessage());
            System.out.println("  状态: 异常 ✗");
        }
    }
    
    private static boolean testTcpConnection(String host, int port) {
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(host, port), 10000);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    private static boolean testDnsResolution(String host) {
        try {
            InetAddress.getByName(host);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    private static boolean testHttpConnection(String host, int port) {
        try {
            URL url = new URL("https://" + host + ":" + port);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(10000);
            connection.setRequestMethod("GET");
            
            int responseCode = connection.getResponseCode();
            return responseCode > 0; // 任何HTTP响应都表示连接成功
        } catch (Exception e) {
            return false;
        }
    }
} 